"""
题目：使用邻接矩阵表示图，实现删除指定顶点的功能（需删除该顶点对应的行和列，以及所有关联的边）。
"""


class GraphMatrix:
    def __init__(self, num_vertices, directed=False):
        self.num_vertices = num_vertices
        self.directed = directed
        self.matrix = [[0 for _ in range(num_vertices)] for _ in range(num_vertices)]

    def add_edge(self, v1, v2, weight=1):
        if 0 <= v1 < self.num_vertices and 0 <= v2 < self.num_vertices:
            self.matrix[v1][v2] = weight
            if not self.directed:
                self.matrix[v2][v1] = weight
        else:
            raise ValueError("顶点编号超出范围")

    def remove_vertex(self, vertex):
        """删除指定顶点（删除对应行和列）"""
        if not (0 <= vertex < self.num_vertices):
            raise ValueError("顶点编号超出范围")

        # 步骤1：删除该顶点对应的行
        self.matrix.pop(vertex)
        # 步骤2：删除所有行中该顶点对应的列
        for row in self.matrix:
            row.pop(vertex)
        # 步骤3：更新顶点总数
        self.num_vertices -= 1

    def display(self):
        """打印邻接矩阵"""
        for row in self.matrix:
            print(row)


# 测试
if __name__ == "__main__":
    graph = GraphMatrix(num_vertices=4)
    graph.add_edge(0, 1)
    graph.add_edge(0, 2)
    graph.add_edge(1, 3)
    graph.add_edge(2, 3)
    print("删除顶点1前的邻接矩阵:")
    graph.display()
    # 输出:
    # [0,1,1,0]
    # [1,0,0,1]
    # [1,0,0,1]
    # [0,1,1,0]

    graph.remove_vertex(1)
    print("\n删除顶点1后的邻接矩阵:")
    graph.display()
    # 输出（顶点编号变为0、2、3）:
    # [0,1,0]
    # [1,0,1]
    # [0,1,0]